//
// ColourTable
// =============
// A Cinema 4D command plugin, providing a color palette
// By Andreas Block, January 23th 2012
// Mail: open@andreasblock.de
//
// Objectives of this project:
//
// Please send any bug reports to e-mail adress above.
//
// Feel free to use the ColourTable plugin and its source to your liking.
// No responsibilities taken for whatsoever, though. 
// If your C4D crashes in combination with this plugin, rather
// send the bugreport together with a description of your parameter
// settings to me (mail adress above) than to Maxon. It's more likely
// my fault than theirs.
// If you modify the source or have any suggestions or feature requests,
// I'd be glad to know about and integrate them.
// And finally: Although you're free to use this plugin where ever you
// like, I'd gladly receive a copy or an example of your work
// (it won't be redistributed without permission). If you're using this
// plugin in a commercial environment and make any profit of it, I kindly
// ask you to donate an amount of your choice (what ever you feel it's
// worth).
//
// Plugin Info:
// ------------
// Tested on: Cinema 4D R12 (Windows, x86 + x86_64)
//            Cinema 4D R13 (Windows, x86)
//
// Version history:
// ----------------
// 00.00.01 | 2012-02-12 | First public release
//
// ToDos / Known bugs:
// - (Job, sticky) Refactor+Cleanup
// - (Job) MAC version
// - (Fix) Value display sometimes incorrect???
// - (Fix, No Fix) Dark greys (<1%) wrong (result always 0%), seems to be a C4D problem
// - (Job) Separate "color reset" from "relayout"?
// - (Feature) Option to hide dialog title and perhaps QuickTab, too
// - (Feature) Inline renaming (change static text labels to edit fields for user doc+c4d)
// - (Feature) Option to show matching, only
// - (Feature) Add undo?
// - (Feature) Add sorting?
// - (Feature) Add some drag and drop support
//             (perhaps drag colors to empty area in scrollgroup to add)
//             Actually more in order to have worked with fdrag and drop, than usefull feature
// - (Feature) Ability to select colors by clicking the name (for deletion)
// - (Feature) Multiple colourtables (like Attribute Manager)
// - (Feature) Own colourtable customgui, instead of hundreds of widgets in scrollgroups
// - (Job) GetParameter/SetParameter
// - (Test) MAC version
//
// Coding style conventions:
// -------------------------
//   Indentation: 2 spaces, NO TABS, NEVER EVER!
//   Comments as C++ single line (with the exception of some macros)
//   Variables:
//   - Variablenames in camel case
//   - Variablenames start lower case letter
//   - Prefixes (combinations allowed):
//     - "g_" for class global variables
//     - "i" for integer (includes Bool and LONG) variables
//     - "p" for pointer or arrays
//     - "idx" for loop counter
//   Constants:
//   - Constantnames all upper case and seperated by "_"
//   - Prefixes:
//     - "ARR_" for arrays
//   - These rules may be overriden by rules for variable for
//     local variables declared constant
//   Functions:
//   - Functionnames in camel case
//   - Functionnames start upper case
//   Macros:
//   - Macronames all upper case and seperated by "_"
//   Jump labels:
//   - Jumplabels all upper case and seperated by "_"
//   - Prefixes:
//     - Upper letters of functionname it's located,
//       not seperated (mandatory!)
//
//------------------------------------------------------------------------
// File: colourtable.h  - class header
//

//========================================================================
// If you modify and recompile this plugin, MAKE SURE YOU GET A NEW
// UNIQUE PLUGIN ID from Plugin Cafe - if you don't your new plugin
// WILL conflict with this one if both are loaded simultaneously!!!!!
//========================================================================
#ifndef _COLOURTABLE_H_
#define _COLOURTABLE_H_

#include "c4d.h"
#include "customgui_quicktab.h"
#include "customgui_bitmapbutton.h"

#include "colourtable_defines.h"
#include "colourtable_types.h"
#include "colourtable_ral_values.h"
#include "colourtable_pantone_values.h"
#include "colourtable_vga_values.h"
#include "colourtable_x11_values.h"
#include "colourtable_css3_values.h"

class ColourTableSubDialog : public SubDialog
{
  INSTANCEOF(ColourTableSubDialog, SubDialog)
private:
  void ScrollGroupMove(const LONG idxColor);

protected:
  BaseContainer *g_pBcPrefs;
  LONG           g_iFlagsDisplay, g_iDlg, g_iNumColors, g_iNumColumns, g_iNumColumnsText, g_idxSelected, g_iFlagsDisplayMask;
  String         g_sCodePrefix;
  const Vector  *g_pArrColors;
  const LONG    *g_pArrCodes;
  Vector         g_colBGHighlight, g_colBGDefault;

  void ColorLabelSet(const LONG idxColor);
  void SelectionSet(LONG idxSelect, Bool iFlagUpdateSearch);
  virtual LONG ColorSearchAndSet(void);
  virtual void CreateDynamicColorTable(void);
  virtual LONG ColorGetCount(void);
  virtual Vector ColorGetVector(const LONG idxColor);
  virtual String ColorGetName(const LONG idxColor);
  virtual LONG ColorGetCode(const LONG idxColor);

public:
  ColourTableSubDialog(void);
  virtual void Init(void);
  virtual Bool CreateLayout(void);
  virtual void ReLayout(void);
  virtual Bool InitValues(void);
  virtual Bool Command(LONG id, const BaseContainer &msg);

  Bool FlagsDisplayToggle(LONG iFlag);
  void FlagsDisplayResetAll(void);
  LONG FlagsDisplayMaskGet(void);
  LONG FlagsDisplayGet(void);
  Bool FlagsDisplayIsSet(LONG iFlag);
  void ColumnsSet(LONG iFlagText, LONG iNumColumns);
};


class ColourTableSubDialogRAL : public ColourTableSubDialog
{
  INSTANCEOF(ColourTableSubDialogRAL, ColourTableSubDialog)
public:
  virtual void Init(void);
};


class ColourTableSubDialogPantone : public ColourTableSubDialog
{
  INSTANCEOF(ColourTableSubDialogPantone, ColourTableSubDialog)
public:
  virtual void Init(void);
};


class ColourTableSubDialogVGA : public ColourTableSubDialog
{
  INSTANCEOF(ColourTableSubDialogVGA, ColourTableSubDialog)
public:
  virtual void Init(void);
};


class ColourTableSubDialogX11 : public ColourTableSubDialog
{
  INSTANCEOF(ColourTableSubDialogX11, ColourTableSubDialog)
public:
  virtual void Init(void);
};


class ColourTableSubDialogCSS3 : public ColourTableSubDialog
{
  INSTANCEOF(ColourTableSubDialogCSS3, ColourTableSubDialog)
public:
  virtual void Init(void);
};


class ColourTableSubDialogGrey : public ColourTableSubDialog
{
  INSTANCEOF(ColourTableSubDialogGrey, ColourTableSubDialog)
private:
  Real    g_fGreyPercent;

  virtual Real GreyCalc(const LONG idxColor);

protected:
  virtual Vector ColorGetVector(const LONG idxColor);
  virtual String ColorGetName(const LONG idxColor);
  virtual LONG ColorGetCode(const LONG idxColor);

public:
  virtual void Init(void);
  virtual Bool InitValues(void);
  virtual Bool Command(LONG id, const BaseContainer &msg);
};


class ColourTableSubDialogUserC4D : public ColourTableSubDialog
{
  INSTANCEOF(ColourTableSubDialogUserC4D, ColourTableSubDialog)
private:
  void ColorAdd(void);
  void ColorDelete(void);

protected:
  Bool g_iFlagChangeDoc;

  virtual BaseContainer* ColorsGetContainer(void);
  virtual LONG ColorGetCount(void);
  virtual Vector ColorGetVector(const LONG idxColor);
  virtual String ColorGetName(const LONG idxColor);
  virtual LONG ColorGetCode(const LONG idxColor);

public:
  virtual void Init(void);
  virtual Bool Command(LONG id, const BaseContainer &msg);
};


class ColourTableSubDialogUserDoc : public ColourTableSubDialogUserC4D
{
  INSTANCEOF(ColourTableSubDialogUserDoc, ColourTableSubDialogUserC4D)
protected:
  virtual BaseContainer* ColorsGetContainer(void);

public:
  virtual void RemoveFromDoc(void);
  virtual void Init(void);
  virtual Bool CoreMessage(LONG id, const BaseContainer &msg);
};


class ColourTableAsyncDialog : public GeDialog
{
  INSTANCEOF(ColourTableAsyncDialog, GeDialog)
private:
  BaseContainer               *g_pBcPrefs;
  BaseBitmap                  *g_pBmIcon;
  QuickTabCustomGui           *g_pGadQuickTab;
  BitmapButtonCustomGui       *g_pGadIcon;
  ColourTableSubDialogRAL      g_sdRAL;
  ColourTableSubDialogPantone  g_sdPantone;
  ColourTableSubDialogVGA      g_sdVGA;
  ColourTableSubDialogX11      g_sdX11;
  ColourTableSubDialogCSS3     g_sdCSS3;
  ColourTableSubDialogGrey     g_sdGrey;
  ColourTableSubDialogUserDoc  g_sdUserDoc;
  ColourTableSubDialogUserC4D  g_sdUserC4D;
  ColourTableSubDialog        *g_pArrSubdialogs[COLOURTABLE_NUM_GROUPS];
  BaseContainer               *g_pBcWeights;
  Bool                         g_iFlagWeightsSaved;
  LONG                         g_iFlagsGroup, g_iNumColumns, g_iNumColumnsActual, g_iNumRows;

  void IconOpen(void); // called in constructor
  void CreateDynamicGroup(void);
  void ReLayout(void);
  void CommandOptions(LONG id);
  
public:
  ColourTableAsyncDialog(void);
  ~ColourTableAsyncDialog(void);
  virtual Bool CreateLayout(void);
  virtual Bool InitValues(void);
  virtual Bool Command(LONG id,const BaseContainer &msg);
  virtual LONG Message(const BaseContainer &msg,BaseContainer &result);
  virtual Bool CoreMessage  (LONG id,const BaseContainer &msg);
};


class ColourTable : public CommandData
{
  INSTANCEOF(ColourTable, CommandData)
private:
  ColourTableAsyncDialog g_dlg;

public:
  virtual Bool Execute(BaseDocument *doc);
  virtual LONG GetState(BaseDocument *doc);
  virtual Bool RestoreLayout(void *secret);
};
#endif _COLOURTABLE_H_